package com.neteast.androidclient.contact.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.neteast.androidclient.contact.R;
import com.neteast.androidclient.contact.Test;
import com.neteast.androidclient.contact.domain.Contact;
import com.neteast.androidclient.contact.widget.ContactPanel;
import com.neteast.androidclient.contact.widget.InfoDetailPanel;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
/**
 * 显示个人信息的页面
 * @author tiangh
 * 2012-3-12 下午5:32:41
 */
public class MeActivity extends Activity implements OnClickListener {
	
	private static final int GET_PHOTO_FROM_GALLERY = 1;
	private static final int TAKING_PICTURE = 2;
	private InfoDetailPanel infoDetail;
	private ImageView contactPhoto;
	private PopupWindow selectPhotoWindow;
	private Contact contact;
	private LinearLayout ll;
	private View view;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		setContentView(R.layout.me);
		init();
		
	}

	private void init() {
//		contact = Test.getContact();
		contact=new Contact();
		infoDetail = new InfoDetailPanel(this, contact);
		view = infoDetail.findViewById(R.id.contact_organization);
		RelativeLayout leftPanel=(RelativeLayout) findViewById(R.id.left_panel);
		leftPanel.addView(infoDetail);
		//目前状态
		final EditText stateContent=(EditText) findViewById(R.id.my_state_content);
		if (!TextUtils.isEmpty(contact.state)) {
			stateContent.setText("    "+contact.state);
		}
		//编辑按钮
		final ToggleButton editInfo = (ToggleButton) infoDetail.findViewById(R.id.btn_edit_info);
		editInfo.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				editInfo.setChecked(isChecked);
				if (isChecked) {
					infoDetail.openEditState();
					stateContent.setEnabled(true);
				}else {
					String value = stateContent.getText().toString().trim();
					infoDetail.contact.state=value;
					stateContent.setText("    "+value);
					infoDetail.closeEditState();
					stateContent.setEnabled(false);
				}
			}
		});
		ll = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.select_photo, null);
		contactPhoto = (ImageView) infoDetail.findViewById(R.id.contact_photo);
		contactPhoto.setOnClickListener(this);
		ll.findViewById(R.id.btn_from_gallery).setOnClickListener(this);
		ll.findViewById(R.id.btn_taking_picture).setOnClickListener(this);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.contact_photo:
			showEditPhotoWindow();
			break;
		case R.id.btn_from_gallery:
			selectPhotoFromGallery();
			break;
		case R.id.btn_taking_picture:
			takingPicture();
			break;
		}
	}
	

	//显示编辑头像的弹出窗
	private void showEditPhotoWindow() {
		boolean b = (Boolean) contactPhoto.getTag();
		if (b) {
			selectPhotoWindow = new PopupWindow(ll,view.getMeasuredWidth(), view.getMeasuredWidth()*2/3, true);
			selectPhotoWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_dialog)); 
			selectPhotoWindow.showAsDropDown(contactPhoto,contactPhoto.getWidth(),-contactPhoto.getHeight());
		}
	}
	//从图库选择照片
	private void selectPhotoFromGallery() {
		Intent intent=new Intent();
		intent.setAction(Intent.ACTION_GET_CONTENT);
		intent.setType("image/*");
		intent.putExtra("crop", "circle");
		intent.putExtra("return-data", true);
		startActivityForResult(intent,GET_PHOTO_FROM_GALLERY);
		selectPhotoWindow.dismiss();
	}
	//拍照
	private void takingPicture() {
		Intent intent=new Intent();
		intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(intent, TAKING_PICTURE);
		selectPhotoWindow.dismiss();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode==Activity.RESULT_OK) {
			switch (requestCode) {
			case GET_PHOTO_FROM_GALLERY:
			case TAKING_PICTURE:
				Bitmap bm = (Bitmap) data.getExtras().get("data");
				if (bm!=null) {
					try {
						saveAndShowPhoto(bm);
					} catch (IOException e) {
						Log.e("test", e.getMessage(),e);
					}
				}
				break;
			}
		}
	}
	public void saveAndShowPhoto(Bitmap bitmap) throws IOException {
		Bitmap photo = ThumbnailUtils.extractThumbnail(bitmap,120,120);
		bitmap.recycle();
		contactPhoto.setImageBitmap(photo);
		File file = createPhotoFile();
		file.getParentFile().mkdirs();
		if (file.exists()) {
			file.delete();
		}
		file.createNewFile();
		FileOutputStream fos=new FileOutputStream(file);
		photo.compress(Bitmap.CompressFormat.PNG, 90, fos);
		contact.photo=file.getAbsolutePath();
		contact.hasPhoto=true;
		Log.i("test", contact.photo);
	}
	
	//创建头像存储的文件
	private File createPhotoFile() {
		String dir=null;
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			dir=Environment.getExternalStorageDirectory().getAbsolutePath();
		}else {
			dir=getFilesDir().toString();
		}
		File file=new File(dir, "photo/"+contact.contactId+".png");
		return file;
	}
}
